<template>
    <div>
        <el-dialog :title="form.id?'修改管理员':'添加管理员'" :visible.sync="dialogVisible" width="38%"
            :before-close="handleClose" center="">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position="left">
                <el-form-item label="账号:" prop="username" style="margin-left:195px">
                    <el-input v-model="form.username" style="width:200px"></el-input>
                </el-form-item>
                <el-form-item label="密码:" prop="pass" style="margin-left:195px">
                    <el-input type="password" v-model="form.pass" autocomplete="off" style="width:200px"></el-input>
                </el-form-item>
                <el-form-item label="确认密码:" prop="checkPass" style="margin-left:195px">
                    <el-input type="password" v-model="form.checkPass" autocomplete="off" style="width:200px">
                    </el-input>
                </el-form-item>
                <el-form-item label="姓名:" prop="name" style="margin-left:195px">
                    <el-input v-model="form.name" style="width:200px"></el-input>
                </el-form-item>
                <el-form-item label="联系方式:" prop="tel" style="margin-left:195px">
                    <el-input v-model="form.tel" style="width:200px"></el-input>
                </el-form-item>
                <el-form-item label="角色:" prop="typeid" style="margin-left:195px">
                    <el-select v-model="form.typeid" placeholder="请选择" style="width:200px">
                        <el-option v-for="item in rolelist" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="qu()">取 消</el-button>
                <el-button type="primary" @click="add()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { role, Add } from '@/api/adminstrator'
export default {
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        handleClickdata: {
            type: Object,
            default: () => { }
        }
    },
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.form.checkPass !== '') {
                    this.$refs.form.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.form.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            form: {

            },
            rules: {
                username: [
                    { required: true, message: '账号不能为空', trigger: 'blur' },
                ],
                pass: [
                    { required: true, message: '密码不能为空', validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { required: true, message: '两次输入密码不一致', validator: validatePass2, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                ],
                tel: [
                    { required: true, message: '电话不能为空', trigger: 'blur' },
                ],
            },
            rolelist: []
        }


    },
    created() {
        this.getrole()
    },
    updated() {
        this.form = this.handleClickdata
    },
    // watch:{
    //     handleClickdata:{
    //         handler(val){
    //             if(val){
    //                 this.form = val
    //                 for(let i in this.form){
    //                     this.form[i] = val[i]
    //                 }
    //             }
    //         }
    //     }
    // },
    methods: {
        // 角色列表
        async getrole() {
            let res = await role()
            if (res.errCode == 10000) {
                this.rolelist = res.data.list
            }
        },
        //添加
        async add() {
            let res = await Add(this.form)
            if (res.errCode == 10000) {
                this.$message.success("添加成功")
                this.form = {}
            }
            this.$emit("isclose", false, true, {})
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    this.$emit("isclose", false, true, {})
                })
                .catch(_ => { });
        },
        qu() {
            this.$emit("isclose", false, true, {})
        }
    }
}
</script>

<style lang="less" scoped>

</style>